﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Meeting.Public;
using Newtonsoft.Json;

namespace Meeting.Web.Admin.Topic
{
    /// <summary>
    /// TopicOperation 的摘要说明
    /// </summary>
    public class TopicOperation : IHttpHandler
    {

        string methodName = "";
        int nPageIndex = 1;
        int nPageSize = 4;

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json;charset=utf-8";

            nPageIndex = CommonFunction.varToIntDef(context.Request.Params["page"], 1);
            nPageSize = CommonFunction.varToIntDef(context.Request.Params["rows"], 15);
            methodName = context.Request.Params["method"];
            switch (methodName)
            {
                case "GetAllTopic":
                    GetAllTopic(context);
                    break;
                case "DeleteTopic":
                    DeleteTopic(context);
                    break;
                case "IsSubmit":
                    IsSubmit(context);
                    break;
                case "UnSubmit":
                    UnSubmit(context);
                    break;
                case "IsDiscuss":
                    IsDiscuss(context);
                    break;
            }
        }


        //获取议题
        private void GetAllTopic(HttpContext context)
        {
            string Name = CommonFunction.removeAttackOfSql(context.Request.Params["Name"]);

            string Begin = context.Request.Params["Begin"];
            string End = context.Request.Params["End"];

            string sF = " 1=1 ";
            if (!string.IsNullOrEmpty(Name)) sF += " And  Title Like '%" + Name + "%' ";

            if (!string.IsNullOrEmpty(Begin)) sF += " And CreateTime >= '" + Begin + "' ";
            if (!string.IsNullOrEmpty(End)) sF += " And  CreateTime <= '" + End + "' ";
            BLL.TopicExt topic = new BLL.TopicExt();
            context.Response.Write(topic.GetAllTopic(nPageIndex, nPageSize, sF));
        }

        //删除议题
        private void DeleteTopic(HttpContext context)
        {
            string topicId = context.Request["Id"];
            BLL.TopicExt bll = new BLL.TopicExt();
            Model.JsonResponse rs = new Model.JsonResponse();
            rs.success = bll.DeleteTopic(topicId);
            rs.Message = bll.errorString;
            context.Response.Write(JsonConvert.SerializeObject(rs));

        }

        public void IsDiscuss(HttpContext context)
        {
            string topicId = context.Request["Id"];
            BLL.TopicExt bll = new BLL.TopicExt();
            Model.JsonResponse rs = new Model.JsonResponse();
            rs.success = bll.IsDiscuss(topicId);
            rs.Message = bll.errorString;
            context.Response.Write(JsonConvert.SerializeObject(rs));
        }

        //提交上会
        private void IsSubmit(HttpContext context)
        {
            string topicId = context.Request["Id"];
            BLL.TopicExt bll = new BLL.TopicExt();
            Model.JsonResponse rs = new Model.JsonResponse();
            rs.success = bll.IsSubmit(topicId);
            rs.Message = bll.errorString;
            context.Response.Write(JsonConvert.SerializeObject(rs));
        }
        //撤销上会
        private void UnSubmit(HttpContext context)
        {
            string topicId = context.Request["Id"];
            BLL.TopicExt bll = new BLL.TopicExt();
            Model.JsonResponse rs = new Model.JsonResponse();
            rs.success = bll.UnSubmit(topicId);
            rs.Message = bll.errorString;
            context.Response.Write(JsonConvert.SerializeObject(rs));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}